/**
Nectarine Utility Project is a software interface for the web system 'demovibes'
Copyright (C) 2010  Alexandre LAURENT

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

website: http://code.google.com/p/nectarine-utility-project/
e-mail: lw.demoscene@gmail.com
**/

#include "onelinertoolbox.h"

#include <QToolButton>
#include <QAction>
#include <QMenu>
#include <QFont>
#include <QColor>
#include <QColorDialog>
#include <QInputDialog>
#include <QMessageBox>
#include <QLineEdit>
#include <QString>

#include <QSignalMapper>

#include <QDebug>

#include "../utils.h"
#include "globals.h"

OnelinerToolBox :: OnelinerToolBox(QWidget* parent/*=0*/)
    :QToolBar(parent)
{
    QFont buttonFont("Helvetica [Cronyx]");

    pSignalMapper = new QSignalMapper(this);
    this->initColourMenu();

    pBoldButton = new QToolButton(this);
    pItalicButton = new QToolButton(this);
    pUnderlinedButton = new QToolButton(this);
    pStrikeButton = new QToolButton(this);

    pColourButton = new QToolButton(this);

    pSongButton = new QToolButton(this);
    pArtistButton = new QToolButton(this);
    pUserButton = new QToolButton(this);
    pGroupButton = new QToolButton(this);
    pCompilationButton = new QToolButton(this);
    pForumButton = new QToolButton(this);
    pThreadButton = new QToolButton(this);
    pFlagButton = new QToolButton(this);

    pBoldAction = new QAction(tr("B"),this);

    buttonFont.setWeight(QFont::Bold);
    pBoldAction->setFont(buttonFont);
    buttonFont.setWeight(QFont::Normal);
    pItalicAction = new QAction(tr("I"),this);
    buttonFont.setItalic(true);
    pItalicAction->setFont(buttonFont);     // Italic
    buttonFont.setItalic(false);
    pUnderlinedAction = new QAction(tr("U"),this);
    buttonFont.setUnderline(true);
    pUnderlinedAction->setFont(buttonFont);
    buttonFont.setUnderline(false);
    pStrikeAction = new QAction(tr("S"),this);
    buttonFont.setStrikeOut(true);
    pStrikeAction->setFont(buttonFont);
    buttonFont.setStrikeOut(false);

    pColourAction = new QAction(QIcon("./data/icons/colours.png"),tr("Colours"),this);
    pColourButton->setMenu(pColourMenu);

    pSongAction = new QAction(QIcon("./data/icons/music.png"),tr("Song"),this);
    pArtistAction = new QAction(QIcon("./data/icons/user_green.png"),tr("Artist"),this);
    pUserAction = new QAction(QIcon("./data/icons/user_green.png"),tr("User"),this);
    pGroupAction = new QAction(QIcon("./data/icons/group.png"),tr("Group"),this);
    pCompilationAction = new QAction(QIcon("./data/icons/compilation.png"),tr("Compilation"),this);
    pForumAction = new QAction(tr("forum"),this);
    pThreadAction = new QAction(tr("thread"),this);
    pFlagAction = new QAction(tr("flag"),this);

    pBoldButton->setDefaultAction(pBoldAction);
    pItalicButton->setDefaultAction(pItalicAction);
    pUnderlinedButton->setDefaultAction(pUnderlinedAction);
    pStrikeButton->setDefaultAction(pStrikeAction);
    pColourButton->setDefaultAction(pColourAction);
    pSongButton->setDefaultAction(pSongAction);
    pArtistButton->setDefaultAction(pArtistAction);
    pUserButton->setDefaultAction(pUserAction);
    pGroupButton->setDefaultAction(pGroupAction);
    pCompilationButton->setDefaultAction(pCompilationAction);
    pForumButton->setDefaultAction(pForumAction);
    pThreadButton->setDefaultAction(pThreadAction);
    pFlagButton->setDefaultAction(pFlagAction);

    // Adding the buttons
    this->addWidget(pBoldButton);
    this->addWidget(pItalicButton);
    this->addWidget(pUnderlinedButton);
    this->addWidget(pStrikeButton);
    this->addWidget(pColourButton);
    this->addSeparator();
    this->addWidget(pSongButton);
    this->addWidget(pArtistButton);
    this->addWidget(pUserButton);
    this->addWidget(pGroupButton);
    this->addWidget(pCompilationButton);
    this->addWidget(pForumButton);
    this->addWidget(pThreadButton);
    this->addWidget(pFlagButton);

    // Connection of the Action to the SLOTs
    connect(pColourAction, SIGNAL(triggered()), pColourButton, SLOT(showMenu()));

    connect(pSongAction, SIGNAL(triggered()), this, SLOT(needSendSongSignal()));
    connect(pArtistAction, SIGNAL(triggered()), this, SLOT(needSendArtistSignal()));
    connect(pUserAction, SIGNAL(triggered()), this, SLOT(needSendUserSignal()));
    connect(pGroupAction, SIGNAL(triggered()), this, SLOT(needSendGroupSignal()));
    connect(pCompilationAction, SIGNAL(triggered()), this, SLOT(needSendCompilationSignal()));
    connect(pForumAction, SIGNAL(triggered()), this, SLOT(needSendForumSignal()));
    connect(pThreadAction, SIGNAL(triggered()), this, SLOT(needSendThreadSignal()));
    connect(pFlagAction, SIGNAL(triggered()), this, SLOT(needSendFlagSignal()));

    qDebug() << "OnelinerToolBox created";
}

OnelinerToolBox :: ~OnelinerToolBox()
{
    qDebug() << "OnelinerToolBox deleted";
}

void OnelinerToolBox :: initColourMenu()
{
    pColourMenu = new QMenu(this);

    QStringList colours;
    colours << "red" << "blue" << "green" << "brown" << "cyan" << "dark blue" << "gold" << "grey" << "magenta" << "orange" << "pink" << "purple" << "white" << "yellow" << "black";

    QStringList::const_iterator itColour;
    for (itColour = colours.constBegin(); itColour!= colours.constEnd(); ++itColour)
    {
        QAction* pTmpAction = Utils::addActionInMenu(this,pColourMenu,QIcon(), (*itColour),"",tr("Inserts") + (*itColour) + tr("bbcode"),pSignalMapper,SLOT(map()));
        pSignalMapper->setMapping(pTmpAction,(*itColour));
    }

    pColourMenu->addSeparator();
    Utils::addActionInMenu(this,pColourMenu,QIcon(ICONSDIR + QString("colours.png")),tr("custom"),"",tr("Inserts custom colour"),this,SLOT(needSendUserColourSignal()));
}

void OnelinerToolBox :: needSendUserColourSignal()
{
    QColor colour;

    qDebug() << "OnelinerToolBox :: needSendUserColourSignal";

    colour = QColorDialog::getColor(Qt::white,this);

    if ( colour.isValid() )
    {
        emit needUserColour(colour);
    }
}

void OnelinerToolBox :: needSendSongSignal(void)
{
    unsigned int id = 0;
    bool isOk = true;

    qDebug() << "OnelinerToolBox :: needSendSongSignal";

    id = QInputDialog::getInt(this,tr("Insert the 'ID' of the song"),tr("Song ID:"),1,1,2147483647,1,&isOk);
    if ( isOk )
    {
        emit needSong(id);
    }
}

void OnelinerToolBox :: needSendArtistSignal(void)
{
    unsigned int id = 0;
    bool isOk = true;

    qDebug() << "OnelinerToolBox :: needSendArtistSignal";

    id = QInputDialog::getInt(this,tr("Insert the 'ID' of the artist"),tr("Artist ID:"),1,1,2147483647,1,&isOk);
    if ( isOk )
    {
        emit needArtist(id);
    }
}

void OnelinerToolBox :: needSendUserSignal(void)
{
    bool isOk = true;

    qDebug() << "OnelinerToolBox :: needSendUserSignal";

    QString name = QInputDialog::getText(this,tr("Insert the nickname of the user"),tr("Nick:"),QLineEdit::Normal,QString(),&isOk);
    if ( isOk && !name.isEmpty() )
    {
        emit needUser(name);
    }
}

void OnelinerToolBox :: needSendGroupSignal(void)
{
    unsigned int id = 0;
    bool isOk = true;

    qDebug() << "OnelinerToolBox :: needSendGroupSignal";

    id = QInputDialog::getInt(this,tr("Insert the 'ID' of the group"),tr("Group ID:"),1,1,2147483647,1,&isOk);
    if ( isOk )
    {
        emit needGroup(id);
    }
}

void OnelinerToolBox :: needSendCompilationSignal(void)
{
    unsigned int id = 0;
    bool isOk = true;

    qDebug() << "OnelinerToolBox :: needSendCompilationSignal";

    id = QInputDialog::getInt(this,tr("Insert the 'ID' of the compilation"),tr("Compilation ID:"),1,1,2147483647,1,&isOk);
    if ( isOk )
    {
        emit needCompilation(id);
    }
}

void OnelinerToolBox :: needSendForumSignal(void)
{
    unsigned int id = 0;
    bool isOk = true;

    qDebug() << "OnelinerToolBox :: needSendForumSignal";

    id = QInputDialog::getInt(this,tr("Insert the 'ID' of the forum"),tr("Forum ID:"),1,1,10,1,&isOk);
    if ( isOk )
    {
        emit needForum(id);
    }
}

void OnelinerToolBox :: needSendThreadSignal(void)
{
    unsigned int id = 0;
    bool isOk = true;

    qDebug() << "OnelinerToolBox :: needSendThreadSignal";

    id = QInputDialog::getInt(this,tr("Insert the 'ID' of the thread"),tr("Thread ID:"),1,1,2147483647,1,&isOk);
    if ( isOk )
    {
        emit needThread(id);
    }
}

void OnelinerToolBox :: needSendFlagSignal(void)
{
    bool isOk = true;

    qDebug() << "OnelinerToolBox :: needSendSongSignal";

    QString flag = QInputDialog::getText(this,tr("Insert the 'ID' of the flag"),tr("Flag ID:"),QLineEdit::Normal,QString(),&isOk);

    if ( isOk && !flag.isEmpty() )
    {
        emit needFlag(flag);
    }
}
